package com.moxiao.yfjh.module.blind.service.alipayLoginService;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.request.AlipayUserUserinfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.api.response.AlipayUserUserinfoShareResponse;

public class AlipayService {

    // 支付宝开发者账号配置信息
    private static final String APP_ID = "2016051801417322";
    private static final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCL8MzjBTvFaBXsSrNIJClEJhrqYOKVQtnFkkuwzfr1XLcF2kJ+BpD6N7zF3VgZiEs56eI05Ju11/9v4Q8JDdVmULt0MMqKEUHxVjfP+0oyDLMv/njCbz+AnQhqB6j/hdVuoqJnaBghpbcxvr8j3qjQee6N+nprdk/WCNwmB/BN31wqoxzc7POHs+LKKaZc56Y8DNzwjMnfrxbkdmkCh3zp+Rspz09d80L6UyTMNm0mTe6uF6TO35d/MW23uiWemzJCJ5zbU1gh6QA8rbRDkSXEGVvXZn/Po4y4qQDPpAYBnJFpBzJ9iB4igC+LYXD5Gqri9uox0hi0KH+OEBlioKq3AgMBAAECggEAM8u3+hsYyXNCEgiAHSaNQ5UbBz8+/mIU+qSwcUt8JM7hS0BCtiS2CncQ3ueTUTf6KABiYpNu8rp06DMT/pTpNIqTJ2lbvgivMKVp2Qg4qRe8RvpYNhc5GOCGmcAmFBW0LtPxanbD38hXawkf9CXYs/k9i713DElpOV5kJYtwrVF9pzal59tiXDIK2ZEu6qAfh2W42eDA3zqKCJD95vJy9OVNGXBt+3hY9qnoceP5f9lrzQqg+DXY5Q8hQcnn7p3/LC19/gQ08krZXOPlUt4eV+cUQDMfP2szwmPr2lnMyyjXOODz7s6jBUZWHwSnWEATxGzMsZ5ob4AF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0U5uyNPboD+TEYEJWqklW/b0qRuH25WlE2PF/RR1Le6O8Imgc84kRq+f5n+AQ7i8zl+CNKNTHvREtlUsLeCagl6efNgM2ywkUnX46O3/r2D1r4oLtLGzuqLkVOK19LHzSPdits05+EdulC+32uT63qnokboICqSdJz41D2JXZOVvkls6842+bFrxFETFCksZN6AuzbiGSlhEwP8mXV5X9AKqEuzysKMdX+NWfq8rQQIDAQAB";

    public static String getMobileByCode(String authcode) {

        try {

        // 创建AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipay.com/gateway.do",
                APP_ID,
                APP_PRIVATE_KEY,
                "json",
                "utf-8",
                ALIPAY_PUBLIC_KEY,
                "RSA2"
        );

        // 获取授权信息
        String authCode = authcode;
        AlipaySystemOauthTokenRequest tokenRequest = new AlipaySystemOauthTokenRequest();
        tokenRequest.setCode(authCode);
        tokenRequest.setGrantType("authorization_code");
        AlipaySystemOauthTokenResponse tokenResponse = alipayClient.execute(tokenRequest);

        // 使用access token获取用户信息
        String accessToken = tokenResponse.getAccessToken();
        AlipayUserUserinfoShareRequest userInfoRequest = new AlipayUserUserinfoShareRequest();
        AlipayUserUserinfoShareResponse userInfoResponse = null;
        userInfoResponse = alipayClient.execute(userInfoRequest, accessToken);

            // 输出用户手机号
//            System.out.println("User Phone Number: " + userInfoResponse.getMobile());
            String zfbOpenId = userInfoResponse.getUserId();

            return userInfoResponse.getMobile();

        } catch (AlipayApiException e) {
            e.getErrMsg();
            throw new RuntimeException(e);
        }
    }

    public static String getOpenIdByCode(String authCode) throws AlipayApiException {
        // 使用支付宝SDK启动OAuth2.0授权的示例代码
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode(authCode);
        request.setGrantType("authorization_code");

        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        String accessToken = response.getAccessToken();

        // 使用访问令牌获取OpenID的示例代码
        AlipayUserInfoShareRequest userInfoRequest = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse userInfoResponse = alipayClient.execute(userInfoRequest, accessToken);
        String openId = userInfoResponse.getUserId();

        return openId;
    }


        public static void main(String[] args) {
            // Initialize Alipay client
            String gatewayUrl = "https://openapi.alipay.com/gateway.do";
            String appId = "9021000135600448";
            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCL8MzjBTvFaBXsSrNIJClEJhrqYOKVQtnFkkuwzfr1XLcF2kJ+BpD6N7zF3VgZiEs56eI05Ju11/9v4Q8JDdVmULt0MMqKEUHxVjfP+0oyDLMv/njCbz+AnQhqB6j/hdVuoqJnaBghpbcxvr8j3qjQee6N+nprdk/WCNwmB/BN31wqoxzc7POHs+LKKaZc56Y8DNzwjMnfrxbkdmkCh3zp+Rspz09d80L6UyTMNm0mTe6uF6TO35d/MW23uiWemzJCJ5zbU1gh6QA8rbRDkSXEGVvXZn/Po4y4qQDPpAYBnJFpBzJ9iB4igC+LYXD5Gqri9uox0hi0KH+OEBlioKq3AgMBAAECggEAM8u3+hsYyXNCEgiAHSaNQ5UbBz8+/mIU+qSwcUt8JM7hS0BCtiS2CncQ3ueTUTf6KABiYpNu8rp06DMT/pTpNIqTJ2lbvgivMKVp2Qg4qRe8RvpYNhc5GOCGmcAmFBW0LtPxanbD38hXawkf9CXYs/k9i713DElpOV5kJYtwrVF9pzal59tiXDIK2ZEu6qAfh2W42eDA3zqKCJD95vJy9OVNGXBt+3hY9qnoceP5f9lrzQqg+DXY5Q8hQcnn7p3/LC19/gQ08krZXOPlUt4eV+cUQDMfP2szwmPr2lnMyyjXOODz7s6jBUZWHwSnWEATxGzMsZ5ob4AFq9MjZlccKQKBgQDhdzYfd1UHaDKEJsnmqdZ1vRKv+870/DZbVb91WccQ/T+gDD7zMHASQwBVsU+CIGl3nYE4UI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O3/r2D1r4oLtLGzuqLkVOK19LHzSPdits05+EdulC+32uT63qnokboICqSdJz41D2JXZOVvkls6842+bFrxFETFCksZN6AuzbiGSlhEwP8mXV5X9AKqEuzysKMdX+NWfq8rQQIDAQAB";

            DefaultAlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, "json", "utf-8", alipayPublicKey, "RSA2");

            // Create auth code request
            AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
            request.setCode("Your_Auth_Code"); // Replace with your auth code
            request.setGrantType("authorization_code");

            try {
                // Execute the request
                AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
                if (response.isSuccess()) {
                    System.out.println("Auth token: " + response.getAccessToken());
                } else {
                    System.out.println("Error: " + response.getSubMsg());
                }
            } catch (AlipayApiException e) {
                throw new RuntimeException(e);
            }
        }




}
